/*
 * Demo.java
 *
 * www.bouthier.net
 * 2001
 */

package ht_birds;

import hypertree.HyperTree;
import hypertree.SwingHTView;

import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JFrame;

import ui.mainWindow;

import dao.BirdDAO;
import dao.BirdVO;
import dao.ConnectionManager;
import dao.PopularNamesDAO;
import dao.PopularNamesVO;

/**
 * The Demo class implements a demo for HyperTree. It display an HyperTree view
 * of a demo tree. The demo tree is build from a file tree, passed in parameter.
 * Demo could take an argument, the path from which start the representing of
 * files. If no arguments is given, the treemap start from the root.
 */
public class hyper_birdsName {

	private static HTDBNode root = null; // the root of the demo tree
	private static HyperTree hypertree = null; // the hypertree builded

	/**
	 * Display a demo HyperTree.
	 * 
	 * @throws SQLException
	 */
	public static void main(String[] args) throws SQLException {
		String pathRoot = null;

		if (args.length > 0) {
			pathRoot = args[0];
		} else {
			pathRoot = "birds";
		}

	//	try {
	//		ConnectionManager.connect(mainWindow.BD_NAME);
	//	} catch (SQLException ex) {
	//		System.out.println("Error while connecting to db: " + ex);
	//	} catch (ClassNotFoundException ex) {
	//		System.out.println("Class not found, check classpath");
	//	}

		root = new HTDBNode("birds");
		for (char letter = 'a'; letter <= 'z'; letter++) {

			Vector<BirdVO> bv_char = BirdDAO.arbitrarySQL("select * from bird where scientific_name like \'"+ letter + "%\'");
			System.out.println("----"+letter+ ": "+ bv_char.size());
			if (bv_char.size() > 0) {
				
				HTDBNode child = new HTDBNode("" + letter + "");
				
				for (int i = 0; i < bv_char.size(); i++) {

					String tname = bv_char.get(i).getScientific_name().split(" ")[0];
					HTDBNode grandson = new HTDBNode(tname);

					Vector<BirdVO> bv_names = BirdDAO.arbitrarySQL("select * from bird where scientific_name like \'"+ tname + "%\'");
					for (int j = 0; j < bv_names.size(); j++) {
						
						
						HTDBNode greatson = new HTDBNode(bv_names.get(j));
						
						Vector<PopularNamesVO> pnames = PopularNamesDAO.selectByBird_scientific_name(bv_names.get(j).getScientific_name());
						
						for(int k=0; k< pnames.size(); k++)
						{
							HTDBNode ggson = new HTDBNode (pnames.get(k).getName());
							greatson.addChild(ggson);
						}								
						
						grandson.addChild(greatson);
					}
					child.addChild(grandson);					
				}			
				
				root.addChild(child);
			}
			
		}

		
		
	//	try {
	//		ConnectionManager.disconnect();
	//	} catch (SQLException e) {
	//		e.printStackTrace();
	//	}

		hypertree = new HyperTree(root);
		SwingHTView view = hypertree.getView();
		// HTNode firstchild = (HTNode) root.children().next();
		// view.translateToOrigin(firstchild);
		JFrame viewFrame = new JFrame("Birds HyperTree");
		viewFrame.setContentPane(view);
		viewFrame.setSize(500, 500);
		// viewFrame.pack();
		viewFrame.setVisible(true);

	}

}
